<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: uploader.php Sat Jul 18 12:52:39 CST 2009
 */
if (false === defined("XOOPS_ROOT_PATH")) {	exit();}

include XOOPS_ROOT_PATH."/class/uploader.php";

class filesUploader extends XoopsMediaUploader
{
    function getUploadDir() {
        return $this->uploadDir;
    }
    
    function mkUploadsDir($srcdir=null, $thumbdir=null){
        global $xoopsModule;
        $srcdir = ( null == $srcdir ) ? $xoopsModule->getVar("dirname") : $srcdir ;
        $thumbdir = ( null == $thumbdir ) ? "thumbs" : $thumbdir;
        $srcdirpath = ( null == $srcdir ) ? XOOPS_VAR_PATH."/".$xoopsModule->getVar("dirname") : XOOPS_VAR_PATH."/".$srcdir;
        $thumbdirpath = ( null == $thumbdir ) ? XOOPS_UPLOAD_PATH."/".$xoopsModule->getVar("dirname") : XOOPS_UPLOAD_PATH."/".$thumbdir;
        
        if ( !is_dir($srcdirpath) ) {
            if ( !@mkdir($srcdirpath, 0777) ){
				return false ;
			} else {
				@chmod($srcdirpath, 0777);
			}
        }
        
        if ( !is_dir($thumbdirpath) ) {
            if ( !@mkdir($thumbdirpath, 0777) ){
				return false ;
			} else {
				@chmod($thumbdirpath, 0777);
			}
        }
        
        $uploadsDir = array(
            "image"=> array(
                    "path"=>$srcdirpath,
                    "dirname"=>$srcdir
                ),
            "thumb"=> array(
                    "path"=>$thumbdirpath,
                    "dirname"=>$thumbdir
                ),
        );
        return $uploadsDir;
    }
    
    function uploadFile($path_upload=null,$num=0,$prefix="") {
	    if ( null == $path_upload ) return false;
	    $prefix = empty($prefix) ? date("Y_m_d") : $prefix ;
 		$media_name = $_POST["xoops_upload_file"][$num] ;
		$image_tmpname = $_FILES[$media_name]["tmp_name"];
		$image_maxsize = $_FILES[$media_name]["size"];
		$image_info = getimagesize($image_tmpname);
		$image_width   = $image_info[0];
		$image_height  = $image_info[1];
		if ( $_POST["MAX_FILE_SIZE"] < $image_maxsize )	 {
			return false;
		}
		$allow_filetype = array("image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png");
	    $uploader = new XoopsMediaUploader($path_upload, $allow_filetype , $image_maxsize);
	    if ($uploader->fetchMedia($media_name)) {
	    	$uploader->setPrefix($prefix."_");
	    	if ($uploader->upload()) {
	    		return $uploader;
	    	}
	    	return false;        
	    }
	    return false;
	}
	
	function setImageThumb($imagePath, $imageName, $thumbPath, $thumbName, $thumbWH = null ){
		if ( null==$thumbWH || !is_array($thumbWH) ) {
			$thumbWH = array("0"=> 60,"1"=> 60);
		}
		$src_file = $imagePath."/".$imageName;
		$new_file = $thumbPath."/".$thumbName;
		
		if ( !filesize($src_file) || !is_readable($src_file)) {
			return false;
		}
		$imginfo = @getimagesize($src_file);
		
		if( $imginfo[0] <= $thumbWH[0] && $imginfo[1] <= $thumbWH[1]) {
			@copy($src_file,$new_file);
			return true;
		}
		
		$newWidth = (int)(min($imginfo[0],$thumbWH[0]));
		$newHeight = (int)($imginfo[1] * $newWidth / $imginfo[0]);
	
		if ( $newHeight > $thumbWH[1] ) {
			$newHeight = (int)(min($imginfo[1],$thumbWH[1]));
			$newWidth  = (int)($imginfo[0] * $newHeight / $imginfo[1]);
		}
		
		$type = $imginfo[2];
		$supported_types = array();
		if (!extension_loaded("gd")) return false;
		if (function_exists("imagegif")) $supported_types[] = 1;
		if (function_exists("imagejpeg"))$supported_types[] = 2;
		if (function_exists("imagepng")) $supported_types[] = 3;
		
		if (in_array($type, $supported_types) )
		{
			switch ($type)
			{
				case 1 :
					if (!function_exists("imagecreatefromgif")) return false;
					$im = imagecreatefromgif($src_file);
					$new_im = imagecreatetruecolor($newWidth, $newHeight);
					ImageCopyResampled($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
					imagegif($new_im, $new_file);
					imagedestroy($im);
					imagedestroy($new_im);
					break;
				case 2 :
					$im = imagecreatefromjpeg($src_file);
					$new_im = imagecreatetruecolor($newWidth, $newHeight);
					imagecopyresampled($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
					imagejpeg($new_im, $new_file, 90);
					imagedestroy($im);
					imagedestroy($new_im);
					break;
				case 3 :
					$im = imagecreatefrompng($src_file);
					$new_im = imagecreatetruecolor($newWidth, $newHeight);
					ImageCopyResampled($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
					imagepng($new_im, $new_file);
					imagedestroy($im);
					imagedestroy($new_im);
					break;
			}
			return true;
		}
		return false;
	}
}